package com.liquidcode.cellular;

import java.util.Vector;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL10Ext;

import com.liquidcode.cellular.R;

import entities.CBaseObject;
import entities.CPlayer;
import entities.CTrail;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.opengl.GLES10Ext;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.util.Log;

public class GLRenderer implements android.opengl.GLSurfaceView.Renderer {
	public GameThread mThread = null;
	private boolean loadTextures = true;
	public Context context = null;
	public float clearColor = 0.0f;
	private int surfaceWidth;
	private int surfaceHeight;


	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		gl.glViewport(0, 0, width, height);

		/****
		 * set up openGL for 2D rendering
		 */
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrthof(0, width, height, 0, 0.0f, 10.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glDisable(GL10.GL_DEPTH_TEST);

		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL10.GL_BLEND);
		loadTextures = false; // reload all texture in case of lost gl context
		surfaceWidth = width;
		surfaceHeight = height;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		loadTextures = false;
		
	}

	public void onDrawFrame(GL10 gl) {
		// clear screen
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		// Check for load textures
		CheckLoadTextures(gl);
		//gl.glMatrixMode(GL10.GL_PROJECTION);
		//gl.glLoadIdentity();
		//GLU.gluOrtho2D( gl, CPlayer.getInstance().getPosX() - surfaceWidth*0.5f, CPlayer.getInstance().getPosX() + surfaceWidth*0.5f, CPlayer.getInstance().getPosY() + surfaceHeight*0.5f, CPlayer.getInstance().getPosY()-surfaceHeight*0.5f);
		// push camera matrix 
		gl.glPushMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glTranslatef((-CPlayer.getInstance().getPosX()) + surfaceWidth*0.5f, (-CPlayer.getInstance().getPosY()) + surfaceHeight*0.5f, 0.0f);
		
		
		//Log.d("Player X: ", String.format("%f", CPlayer.getInstance().getPosX()));
		//Log.d("Player Y: ", String.format("%f", CPlayer.getInstance().getPosY()));
		
		if(GameInfo.getInstance().GameInitialized == true ) {
			
			// render the map 
			CMapManager.getInstance().RenderMap(gl);
			CTrailManager.getInstance().Render(gl);
			// render enemies and objects 
			CEnemyManager.getInstance().RenderEnemies(gl);
			CBulletManager.getInstance().RenderBullets(gl);
			// Render the player
			CPlayer.getInstance().Render(gl);
			// render the joysticks
			//CInputManager.getInstance().Render(gl);
			
		}
		// pop camera matrix
		gl.glPopMatrix();
	}

	private void CheckLoadTextures(GL10 gl) {
		if (loadTextures == false) {
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.temp_health);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.temp_mana);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.temp_enemy);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.temp_attack);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.enemy_shot);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.player_cell_temp);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.joy_back);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.joy_button);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.map_grid_overlay);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.map_star_overlay);
			CAssetManager.GetInstance().loadTexture(gl, context,
					R.drawable.enemy_cell_temp);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.test_animation);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.health_green);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.health_red);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.shock_temp);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.double_shot);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.triple_shot);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.spread_shot);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.flame_shot);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.shield_image);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.drain_bar_front);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.drain_bar_back);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.trail_image);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.trigger_shell_image);
			CAssetManager.GetInstance().loadTexture(gl,  context, R.drawable.trigger_shrapnel_image);
			
			// load player texture
			Texture t = CAssetManager.GetInstance().getTexture(
					R.drawable.test_animation);

			CPlayer.getInstance().getSprite().setAnimTexture(t);
			CPlayer.getInstance().setScreen(surfaceWidth, surfaceHeight);
			// input manager
			CInputManager.getInstance().InitManagerWithScreen(surfaceWidth, surfaceHeight);
			
			// map manager
			t = CAssetManager.GetInstance().getTexture(
					R.drawable.map_grid_overlay);
			CMapManager.getInstance().getMapTexture().setTexture(t, true);
			t = CAssetManager.GetInstance().getTexture(
					R.drawable.map_star_overlay);
			CMapManager.getInstance().getMapOverlayTexture().setTexture(t, true);
			CMapManager.getInstance().InitMapWithScreen(surfaceWidth, surfaceHeight);			
			
			GameInfo.getInstance().GameInitialized = true;
			loadTextures = true;
		}
	}
	
	public synchronized void stopThread()
	{
		if( mThread != null )
		{
			Thread stopThread = mThread;
			mThread = null;
			stopThread.interrupt();
		}
	}
	
	public GameThread getGameThread() { return mThread; }
}
